//
//  IAQuestionListCell.m
//  VoiceClass
//
//  Created by caoxiaobo on 2018/1/11.
//  Copyright © 2018年 YRX. All rights reserved.
//

#import "IAQuestionListCell.h"
#import "IASubjectItem.h"
#import "IAStudent.h"
#import "Definition.h"
#import "PNChart.h"

static CGFloat const qFontSize = 18.0f;
static CGFloat const aFontSize = 15.0f;
static CGFloat const qLRPadding = 12.0f;

#define CMColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define kQuestionListCellChartW ([UIScreen mainScreen].bounds.size.width - 48.0f)
#define kQuestionListCellChartH 180.0f

@implementation IAQuestionListItem

- (void)setSubjectItem:(IASubjectItem *)subjectItem {
    _subjectItem = subjectItem;
    
    CGFloat viewW = [UIScreen mainScreen].bounds.size.width;
    UIFont *qFont = [UIFont systemFontOfSize:qFontSize];
    CGFloat qX = qLRPadding * 2;
    CGFloat qY = qLRPadding;
    CGFloat qW = viewW - 2 * qLRPadding;
    CGFloat qH = [subjectItem.question boundingRectWithSize:CGSizeMake(qW, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName : qFont}
                                            context:nil].size.height;
    _questionFrame = CGRectMake(qX, qY, qW, qH);
    
    CGFloat const aH = 22.0f;
    _answerFrame = CGRectMake(qX,
                              CGRectGetMaxY(_questionFrame) + qLRPadding,
                              qW,
                              aH);
    _barLabelFrame = CGRectMake(qX,
                                CGRectGetMaxY(_answerFrame) + qLRPadding,
                                kQuestionListCellChartW,
                                aH);
    if (self.students.count <= 0) {
        _barChartFrame = CGRectZero;
    } else {
        _barChartFrame = CGRectMake(qX,
                                    CGRectGetMaxY(_barLabelFrame),
                                    kQuestionListCellChartW,
                                    kQuestionListCellChartH);
        _colors = @[PNGreen, PNRed, PNGreen, PNRed, PNGreen];
    }
    
    CGFloat const lineH = 0.5f;
    _bottomLineFrame = CGRectMake(0, CGRectGetMaxY(_barChartFrame) + qLRPadding, viewW, lineH);
    
    _cellHeight = (qLRPadding + qH
                   + qLRPadding + aH
                   + aH
                   + qLRPadding + lineH);
    
    if (self.students.count > 0) {
        _cellHeight += (qLRPadding + kQuestionListCellChartH);
    }
}

@end

@interface IAQuestionListCell ()

@property (nonatomic, strong) UILabel *qLabel;
@property (nonatomic, strong) UILabel *aLabel;
@property (nonatomic, strong) UILabel *barLabel;
@property (nonatomic, strong) PNBarChart *barChart;
@property (nonatomic, strong) UIView *bottomLineView;

@end

@implementation IAQuestionListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _qLabel = [self assembleLabel:[UIFont boldSystemFontOfSize:qFontSize] textColor:[UIColor darkGrayColor] numLines:0];
        _aLabel = [self assembleLabel:[UIFont systemFontOfSize:aFontSize] textColor:HEXCOLOR(0x999999) numLines:1];
        
        _barLabel = [self assembleLabel:[UIFont systemFontOfSize:aFontSize] textColor:PNGreen numLines:1];
        _barLabel.text = @"取每个学生得分最高的一次";
        _barLabel.backgroundColor = CMColor(236, 235, 243);
        _barChart = [[PNBarChart alloc] initWithFrame:CGRectMake(0,
                                                                 0,
                                                                 kQuestionListCellChartW,
                                                                 kQuestionListCellChartH)];
        _barChart.backgroundColor = CMColor(236, 235, 243);
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = [UIColor lightGrayColor];
        
        [self.contentView addSubview:_qLabel];
        [self.contentView addSubview:_aLabel];
        [self.contentView addSubview:_barLabel];
        [self.contentView addSubview:_barChart];
        [self.contentView addSubview:_bottomLineView];
    }
    return self;
}

- (UILabel *)assembleLabel:(UIFont *)font textColor:(UIColor *)tColor numLines:(NSInteger)numLines {
    UILabel *aLabel = [UILabel new];
    aLabel.textColor = tColor;
    aLabel.numberOfLines = numLines;
    aLabel.font = font;
    return aLabel;
}

- (void)setItem:(IAQuestionListItem *)item {
    _item = item;
    self.qLabel.text = [NSString stringWithFormat:@"Q: %@", item.subjectItem.question];
    self.qLabel.frame = item.questionFrame;
    
    self.aLabel.text = [NSString stringWithFormat:@"A: %@", item.subjectItem.answer];
    self.aLabel.frame = item.answerFrame;
    
    self.barLabel.frame = item.barLabelFrame;
    
    if (item.names.count <= 0 || item.rates.count <= 0) {
        self.barChart.hidden = YES;
    } else {
        self.barChart.hidden = NO;
        self.barChart.frame = item.barChartFrame;
        self.barChart.yChartLabelWidth = 20.0;
        self.barChart.xLabelWidth = 20.0;
        self.barChart.chartMarginLeft = 30.0;
        self.barChart.chartMarginRight = 10.0;
        self.barChart.chartMarginTop = 5.0;
        self.barChart.chartMarginBottom = 10.0;
        self.barChart.labelMarginTop = 5.0;
        self.barChart.showChartBorder = YES;
        self.barChart.labelTextColor = PNRed;
//        [self.barChart setStrokeColors:item.colors];
        self.barChart.xLabels = item.names;
        self.barChart.yLabels = @[@0, @50, @100];
        self.barChart.yValues = item.rates;
        self.barChart.isGradientShow = NO;
        self.barChart.isShowNumbers = NO;
    }
    
    [self.barChart strokeChart];
    self.bottomLineView.frame = item.bottomLineFrame;
}

@end
